package com.study.manage.mvc.v2;

import java.util.Arrays;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.study.manage.dal.UserRepository;
import com.study.manage.domain.User;
import com.study.manage.domain.enumm.LiveDieState;
import com.study.manage.dto.AjaxResponse;
import com.study.manage.dto.SessionUser;
import com.study.manage.mvc.common.UploadController;
import com.study.manage.utils.MapUtils;


@Controller
@RequestMapping("/v2/org")
public class V2OrgController extends UploadController {


    @RequestMapping(path = "list", method = RequestMethod.GET)
    public String article(HttpServletRequest request, ModelMap model,
            HttpServletResponse response) {
        return "v2/org/list";
    }


    @Autowired
    private UserRepository userRepository;

    @ResponseBody
    @RequestMapping(path = "list-list", method = RequestMethod.GET)
    public AjaxResponse list_list(HttpServletRequest request, String name, Long id,
            Integer parentId,
            @PageableDefault(sort = {"id"}, direction = Direction.DESC) Pageable pageable) {
        AjaxResponse ajax = new AjaxResponse(true);
        name = StringUtils.isEmpty(name) ? null : name;
        Page<User> page = userRepository.findByIdIn(Arrays.asList(4210321l, 4210888l,3767820l, 265664l, 265633l,
                266055l, 266426l, 265382l, 266373l, 266599l, 266389l, 265441l, 266438l, 265581l,
                265596l, 265445l, 266394l, 258019l, 260975l, 246762l, 258522l, 261122l, 256805l,
                260936l, 249112l, 256788l, 283996l, 263783l, 261384l, 262542l, 262100l, 4228708l),


                pageable);

        return ajax.setObj(page.map(new Converter<User, Map<String, Object>>() {
            @Override
            public Map<String, Object> convert(User source) {
                Map<String, Object> map = MapUtils.convert(source);
                return map;
            }
        }));
    }


    @ResponseBody
    @RequestMapping(path = "list-switchUser", method = RequestMethod.POST)
    public AjaxResponse list_switchUser(HttpServletRequest request, Long id, LiveDieState state,
            @PageableDefault(sort = {"createdAt"}, direction = Direction.DESC) Pageable pageable) {
        AjaxResponse ajax = new AjaxResponse(true);
        User org = userRepository.findOne(id);
        SessionUser su = getUserInfo();
        su.setId(org.id);
        su.setName(org.name);
        su.setNumber(org.number);
        su.setType(org.type);
        su.setParentId(org.parentId == null ? org.id : org.parentId);
        session.setAttribute("user", su);
        return ajax;
    }

}
